package day01;

import lombok.extern.slf4j.Slf4j;

public class interruptTest2 {
    public static void main(String[] args) throws InterruptedException {
        twoMode twoMode = new twoMode();
        twoMode.start();

        Thread.sleep(5000);
        twoMode.stop();
    }
}

@Slf4j
class twoMode{
    // 两阶段线程
    private Thread monitorThread;

    void start(){
        monitorThread = new Thread(()->{
            while(true){
                Thread current = Thread.currentThread();
                if(current.isInterrupted()){
                    log.debug("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.debug("monitor");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    // 设置打断标记为true
                    current.interrupt();
                }
            }
        },"monitor");

        monitorThread.start();
    }


    void stop(){
        monitorThread.interrupt();
    }
}
